<?php

namespace app\Cron\controller;

use app\api\model\PushBind;
use app\cron\model\Driver;
use \think\Db;

class Order
{
    public function dispatch()
    {
        // 通知规律有近到远  最远3000米
        $radius     = [100, 500, 1000, 2000, 3000];
        // 获取待派车订单
        $orderField = 'order_id,order_state,order_type,use_type,booking_type,order_time,0 car_type_id';
        $gpsField   = 'from_addr departure,from_lon,from_lat,to_addr destination,to_lon,to_lat,plan_time';
        $order      = Db::view('order', [$orderField])
            ->view('order_gps', [$gpsField], 'order.order_id = order_gps.order_id', 'left')
            ->where("order_state", 4)
            ->select();
        foreach ($order as $v) {
            $tempArr = [];// 过滤重复推送
            foreach ($radius as $ra) {
                list($lonmin, $lonmax, $latmin, $latmax) = getAround($v['from_lon'], $v['from_lat'], $ra);
                $where['lon']        = ['between', "$lonmin,$lonmax"];
                $where['lat']        = ['between', "$latmin,$latmax"];
                $where['user_id']    = ['>', 0];
                $where['work_state'] = ['in','2,3'];
                $driver              = Driver::where($where)->where(['user_id' => ['notin', implode(',', $tempArr)]])->column('user_id');
                $tempArr             = array_merge($tempArr, $driver);
                if (empty($driver)) {
                    continue;
                }
                $pushwhere['user_id'] = ['in', implode(',', $driver)];
                $device               = PushBind::where($pushwhere)->column('push_user_id');
                $msg                  = json_encode($v);
                (new \app\base\logic\Push())->pushDriver($device, $msg);
            }
        }
    }
}
